
from fastapi import FastAPI
import sys 
import os 
# 加载项目目录
sys.path.append(os.path.realpath('')) 
from app.datahandle import blogdata
from  pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI() # 创建 api 对象
origins = [

    "http://localhost:4395"

]


app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# 定义请求模型 
class Bloginfo(BaseModel):
    name :str = "demo"
    url : str = "demo"





@app.get("/") # 根路由
def root():
    return {"hello": "world"}

@app.get("/say/{data}")
def say(data: str,q: int):
    return {"data": data, "item": q}

@app.get("/getblogurl")
def getblogurl():
    return blogdata.get_blog_url_all()

@app.post("/addblogurl")
def getblogurl(bloginfo:Bloginfo):
    return blogdata.insert_url_blog_set(bloginfo.name,bloginfo.url)

if __name__ == '__main__':
    print("博客之家。。。")